package org.example.java_gobang.game;

import org.springframework.stereotype.Component;
import java.util.concurrent.ConcurrentHashMap;

//游戏房间管理器:
@Component
public class RoomManager {
    //将创建的游戏房存储到队列中:
    //key:roomId val:room对象
    private ConcurrentHashMap<String,Room> rooms = new ConcurrentHashMap<>();
    // 用户Id->用户所在的房间Id的映射
    //key:userId   val:用户所在的rootId
    private ConcurrentHashMap<Integer,String> userIdToroomId = new ConcurrentHashMap<>();
    //将游戏房间加入队列:
    public void add(Room room, int userId1,int userId2){
        rooms.put(room.getRoomId(),room);
        userIdToroomId.put(userId1,room.getRoomId());
        userIdToroomId.put(userId2,room.getRoomId());
    }
    //将游戏房间从队列中移除:
    public void remove(String roomId,int userId1,int userId2){
        rooms.remove(roomId);
        userIdToroomId.remove(userId1);
        userIdToroomId.remove(userId2);
    }
    //根据用户Id,获取房间信息
    public Room getRoomByUserId(int userId){
        String roomId = userIdToroomId.get(userId);
        if(roomId == null){
            return null;
        }
        return rooms.get(roomId);
    }
    //根据房间id,获取房间信息
    public Room getRoomByroomId(String roomId){
        return rooms.get(roomId);
    }

}
